// pages/enter/enter.js
const app = getApp()
import Toast from '@vant/weapp/toast/toast';
Page({

    /**
     * 页面的初始数据
     */
    data: {
      serviceId:1,
      num:'',
      index:1,
      count:0,
      timer:null,
      showAlert:false,
      uId:0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {
      var that = this;
      that.selectComponent("#loading").show()
      const cardId = wx.getStorageSync('cardId')
      if(cardId){
        wx.redirectTo({
          url: '../index/index?cardId='+cardId,
        })
      }else{
        app.getLogUser(function (uInfo) {
            const {userId} = uInfo 
            that.setData({
              userId:userId
            })
            that.getUserCard(userId) 
        });
      }
    },

    getNum(){
        var that = this;
        wx.request({
            url: app.globalData.apiUrlNew + 'Mini/BalUpdated.ashx?action=activateNum',
            data: {},
            dataType: 'json',
            header: {
              'content-type': 'application/json' // 默认值
            },
            success(resroom) {
              that.selectComponent("#loading").hide()
              var result = resroom.data;
              console.log(result)
              if (result.getNum == 0) {
                that.countNum(result.objCard.num)
                that.countNumber()
              } 
            }
          })
    },
    countNum(num){
        const that = this
        const op = this.data.index === 1? num*100:num
        const numCount = Math.round(Math.random()*100)
        const xx = Number(numCount) + Number(op)
        that.setData({
            num:  xx,
            index: that.data.index+1
        })
        if(that.data.count === 20){
            clearInterval(that.data.timer)
        }
       
    },
    countNumber(){
        const that = this
        that.data.timer = setInterval(()=>{
            that.data.count = that.data.count +1 
            that.countNum(that.data.num)
        },2000) 
    },
    handleClick(){
      this.getUserInfo()
    //   wx.navigateTo({
    //     url: '../bindCard/bindCard',
    // })
    },
    getUserCard(userId){
      const that = this
        wx.request({
            url: app.globalData.apiUrl + 'Mini/BalCard.ashx?action=9',
            data: {
              userId: userId
            },
            dataType: 'json',
            header: {
              'content-type': 'application/json' // 默认值
            },
            success(resroom) {
              var result = resroom.data;
              console.log(result)
              if (result.getNum == 0) {
                  if(result.listMyCard.length>0){
                    const cardId= result.listMyCard[0].card_id
                    wx.redirectTo({
                      url: '../index/index?cardId='+cardId,
                    })
                  }else{
                    that.getNum()
                  }
              } 
            }
          })
    },
    getUserInfo() {
        // 推荐使用 wx.getUserProfile 获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
        var that=this;
        wx.getUserProfile({
          desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
          success: (res) => {
            console.log(res)
            const{avatarUrl,nickName}=res.userInfo;  
            wx.request({
              url: app.globalData.apiUrl+'Mini/BalCard.ashx?action=2',
              data: {
                uAvatar: avatarUrl,
                uName: nickName,
                uId: that.data.userId,
              },
              dataType: 'json',
              header: {
                'content-type': 'application/json' // 默认值
              },
              success(resroom) {
                console.log(resroom)
                var resData = resroom.data;
                if (resData.getNum == 0) {
                  that.setData({
                    showAlert: true
                  })
                  //that.data.showAlert = true
                //   wx.setStorageSync('sto_userinfo', "-");
                //   wx.navigateTo({
                //      url: '../bindCard/bindCard',
                //  })
                } else {
                     console.log('获取用户信息失败')
                }
              },
              fail(resfail) {
                console.log(resfail)
              }
            })
          },
          fail:function(e){
              console.log(e)
          }
        })
      },
      getPhoneNumber(e){
        this.setData({
          showAlert: false
        })
        var bb=e.detail.code;
        var that=this;
        wx.request({
          url: app.globalData.apiUrl+'Mini/BalCard.ashx?action=3',
          data: {
            jsCode: bb,
            userId: that.data.uId,
          },
          dataType: 'json',
          header: {
            'content-type': 'application/json' // 默认值
          },
          success(res) {
            console.log(res)
            var respd = res.data;
            if(respd.getNum==0){  
              wx.setStorageSync('sto_userinfo', "-");
              wx.navigateTo({
                url: '../bindCard/bindCard',
              })
            }else{
              Toast(respd.getMsg)
            }
          },
          fail(resfail) {
            Toast(resfail.data)
          }
        })  
      },
      cancelPhone(){
        this.setData({
          showAlert: false
        })
      } ,
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})